// pages/team/detail/teamDetail.js
const api = require("../../../utils/api");
const layer = require("../../../utils/layer");
const listTouchBehavior = require("../../../utils/listTouchBehavior");
Page({
    // 混入列表左移
    behaviors: [listTouchBehavior],
    /**
     * 页面的初始数据
     */
    data: {
        name: "xxx",
        id: '0',
        detail: {}, // 详情信息
        fields: [
            {
                title: "id",
                field: "id",
                type: 'input',
                disable: true,
                hide: true
            },
            {
                title: "团队名称",
                field: "name",
                type: 'input',
            },
            {
                title: "创建者",
                field: "createByNickName",
                type: 'input',
                disable: true
            },
            {
                title: "创建时间",
                field: "createTime",
                type: 'date',
                disable: true
            },
        ],
        watchFields: ["name"],
        users: [], // 团队中的成员
        canEdit: false, // 只有团队的创建者可以编辑团队信息
        form: {}, // 团队表单
        isEdit: false, // 是否进行了编辑操作
        deleteUser: [], // 要删除的用户
        isInit: true, // 是否为初始化
        userId: wx.getStorageSync('userId')
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let name = options.name;
        let id = options.id;
        this.setData({
            name, id
        });
        this.init();
    },
    // 初始化，获得团队详情
    init() {
        let that = this
        api.get("/team/detail", { id: this.data.id }).then(res => {
            if (res.data) {
                let fields = that.data.fields
                let canEdit = res.data.canEdit;
                for (let field of fields) {
                    field.value = res.data[field.field]
                    if (!canEdit) {
                        field.disable = true;
                    }
                }
                this.setData({
                    fields,
                    canEdit,
                    users: res.data.users,
                })
            }
        });
    },
    // 表单修改
    voucherChange(e) {
        this.setData({
            form: e.detail.form,
            isEdit: true
        })
    },
    // 删除团队成员
    deleteUser(e) {
        let that = this;
        let id = e.currentTarget.dataset.id
        let nickName = e.currentTarget.dataset.nickName
        layer.showModal(() => {
            wx.showLoading();
            let users = that.data.users
            let teamId = that.data.id;
            api.post("/team/delete/user", {
                "teamId": teamId,
                "users": [id]
            }).then(res => {
                wx.hideLoading();
                // 删除users中对应id的user
                users.some((item, i) => {
                    if (item.id === id) {
                        users.splice(i, 1)
                    }
                })
                that.setData({
                    users,
                })
            });
        }, "提示", `确定要删除成员【${nickName}】吗？`);
    },
    /**
     * 提交修改
     */
    submit() {
        let that = this;
        layer.showModal(() => {
            layer.showLoading();
            let form = that.data.form;
            // 更新团队信息
            api.post("/team/save", { id: form.id, name: form.name }).then(res => {
                wx.hideLoading();
                layer.showToast(res.message)
                setTimeout(() => {
                    that.cancle();
                }, 500);
            });
        }, '提示', "确定修改团队信息？")
    },
    /**
     * 取消修改
     */
    cancle() {
        wx.navigateBack();
    },
    // 去团队成员邀请页
    toIvitePage() {
        wx.navigateTo({ url: `/pages/team/invite/invite?id=${this.data.id}&teamName=${this.data.name}` });
    },
})
